package com.qxsx.midi.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.exam.ExamWaitAcitvity;
import com.qxsx.midi.activity.exam.ExamWaitFailActivity;
import com.qxsx.midi.activity.exam.ExamWaitPayActivity;
import com.qxsx.midi.activity.exam.ExamWaitSureActivity;
import com.qxsx.midi.activity.exam.ExamWaitYesAcitvity;
import com.qxsx.midi.activity.home.MusicDetailsActivity;
import com.qxsx.midi.model.Exam;
import com.qxsx.midi.model.ExamCate;
import com.qxsx.midi.utils.MyNet;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Zrj on 2015/8/31.
 */
public class ExamMyItemAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private List<Exam> list = new ArrayList<>();
    private Context context;
    private Activity activity;

    public ExamMyItemAdapter(Context context, List<Exam> list, Activity activity) {
        this.context = context;
        this.list = list;
        this.activity = activity;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void AddData(List<Exam> data) {
        for (Exam em : data) {
            list.add(em);
        }
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.exam_mylist_item, null);
            holder.img_item = (ImageView) convertView.findViewById(R.id.img_exam_icon);
            holder.tv_name = (TextView) convertView.findViewById(R.id.tv_exam_name);
            holder.tv_pos = (TextView) convertView.findViewById(R.id.tv_exam_pos);
            holder.tv_time = (TextView) convertView.findViewById(R.id.tv_exam_time);
            holder.tv_state_info = (TextView) convertView.findViewById(R.id.tv_state_info);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final Exam exam = list.get(position);
        holder.tv_name.setText(exam.get_name());
        holder.tv_pos.setText("考试地点：" + exam.get_addr());
        holder.tv_time.setText("考试时间：" + exam.get_timer());
        holder.tv_state_info.setText(exam.get_mesg());
        holder.img_item.setImageResource(exam.get_url());


        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (exam.get_code().equals("1")) {
                    //已报名
                    //等待考试
                  Intent it = new Intent(activity, ExamWaitYesAcitvity.class);
                    it.putExtra("id", exam.get_id());//传递商家ID
                    activity.startActivity(it);
                }
                if (exam.get_code().equals("2")) {
                    //审核通过（报名通过）
                    Intent it = new Intent(activity, ExamWaitPayActivity.class);
                    it.putExtra("id", exam.get_id());//传递商家ID
                    activity.startActivity(it);
                }
                if (exam.get_code().equals("3")) {
                    //等待考试
                    Intent it = new Intent(activity, ExamWaitAcitvity.class);
                    it.putExtra("id", exam.get_id());//传递商家ID
                    activity.startActivity(it);
                }
                if (exam.get_code().equals("4")) {
                    //考试未通过
                    Intent it = new Intent(activity, ExamWaitFailActivity.class);
                    it.putExtra("id", exam.get_id());//传递商家ID
                    activity.startActivity(it);
                }
                if (exam.get_code().equals("5")) {
                    //考试已通过
                    Intent it = new Intent(activity, ExamWaitSureActivity.class);
                    it.putExtra("id", exam.get_id());//传递商家ID
                    activity.startActivity(it);
                }
            }
        });


        return convertView;
    }

    class ViewHolder {
        ImageView img_item;
        TextView tv_name;
        TextView tv_pos;
        TextView tv_time;
        TextView tv_state_info;
    }
}
